package com.web.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;

import org.apache.commons.io.FilenameUtils;

public class FileUtils extends FilenameUtils {
	
	public static boolean copyInputStreamToFile(long size, InputStream in, File dest) throws IOException{
		
		//创建目录
		if(!dest.getParentFile().exists()){
			dest.getParentFile().mkdirs();
		}
		
		//复制文件
		FileOutputStream des = null;
		FileChannel desCh = null;
		ReadableByteChannel srcCh = Channels.newChannel(in);
		
		des = new FileOutputStream(dest);
		desCh = des.getChannel();

		// 通道复制
		desCh.transferFrom(srcCh, 0, size);

		boolean result = dest.exists() && (size == desCh.size());

		// 关闭通道
		in.close();
		des.close();
		srcCh.close();
		desCh.close();
		
		return result;
	}
}
